﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace BOSS.Web.Extensions
{
    /// <summary>
    /// HtmlHelper 的链接扩展
    /// </summary>
    public static class LinkExtensions
    {
        /// <summary>
        /// 链接目标名称
        /// </summary>
        private const string TargetName = "_blank";

        /// <summary>
        /// 点击事件处理器
        /// </summary>
        private const string ClickHandler = "return openLink(this);";

        /// <summary>
        /// 添加超链接，页面将在新标签页打开
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <returns></returns>
        public static string ActionLinkInNewTab(this HtmlHelper htmlHelper, string linkText, string actionName)
        {
            return htmlHelper.ActionLink(linkText, actionName, null, new { target = TargetName, onclick = ClickHandler });
        }

        /// <summary>
        /// 添加超链接，页面将在新标签页打开
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static string ActionLinkInNewTab(this HtmlHelper htmlHelper, string linkText, string actionName, object values)
        {
            return htmlHelper.ActionLink(linkText, actionName, values, new { target = TargetName, onclick = ClickHandler });
        }

        /// <summary>
        /// 添加超链接，页面将在新标签页打开
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static string ActionLinkInNewTab(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary values)
        {
            return htmlHelper.ActionLink(linkText, actionName, values, new RouteValueDictionary(new { target = TargetName, onclick = ClickHandler }));
        }

        /// <summary>
        /// 添加超链接，页面将在新标签页打开
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="controllerName"></param>
        /// <returns></returns>
        public static string ActionLinkInNewTab(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
        {
            return htmlHelper.ActionLink(linkText, actionName, controllerName, null, new { target = TargetName, onclick = ClickHandler });
        }

        /// <summary>
        /// 添加超链接，页面将在新标签页打开
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="values"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static string ActionLinkInNewTab(this HtmlHelper htmlHelper, string linkText, string actionName, object values, object htmlAttributes)
        {
            RouteValueDictionary htmlAttr = new RouteValueDictionary(htmlAttributes);
            htmlAttr["target"] = TargetName;
            if (htmlAttr.ContainsKey("tabTitle"))
            {
                htmlAttr["onclick"] = string.Format("return openLink(this, \"{0}\");", htmlAttr["tabTitle"]);
                htmlAttr.Remove("tabTitle");
            }
            else
            {
                htmlAttr["onclick"] = ClickHandler;
            }
            return htmlHelper.ActionLink(linkText, actionName, new RouteValueDictionary(values), htmlAttr);
        }

        /// <summary>
        /// 添加超链接，页面将在新标签页打开
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="values"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static string ActionLinkInNewTab(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary values, RouteValueDictionary htmlAttributes)
        {
            htmlAttributes["target"] = TargetName;
            if (htmlAttributes.ContainsKey("tabTitle"))
            {
                htmlAttributes["onclick"] = string.Format("return openLink(this, \"{0}\");", htmlAttributes["tabTitle"]);
                htmlAttributes.Remove("tabTitle");
            }
            else
            {
                htmlAttributes["onclick"] = ClickHandler;
            }
            return htmlHelper.ActionLink(linkText, actionName, values, htmlAttributes);
        }

        /// <summary>
        /// 添加超链接，页面将在新标签页打开
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="controllerName"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static string ActionLinkInNewTab(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object values)
        {
            RouteValueDictionary htmlAttr = new RouteValueDictionary();
            htmlAttr["target"] = TargetName;
            htmlAttr["onclick"] = ClickHandler;
            return htmlHelper.ActionLink(linkText, actionName, controllerName, new RouteValueDictionary(values), htmlAttr);
        }

        /// <summary>
        /// 添加超链接，页面将在新标签页打开
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="controllerName"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static string ActionLinkInNewTab(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary values)
        {
            RouteValueDictionary htmlAttributes = new RouteValueDictionary();
            htmlAttributes["target"] = TargetName;
            htmlAttributes["onclick"] = ClickHandler;
            return htmlHelper.ActionLink(linkText, actionName, controllerName, values, htmlAttributes);
        }

        /// <summary>
        /// 添加超链接，页面将在新标签页打开
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="controllerName"></param>
        /// <param name="values"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static string ActionLinkInNewTab(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object values, object htmlAttributes)
        {
            RouteValueDictionary htmlAttr = new RouteValueDictionary(htmlAttributes);
            htmlAttr["target"] = TargetName;
            if (htmlAttr.ContainsKey("tabTitle"))
            {
                htmlAttr["onclick"] = string.Format("return openLink(this, \"{0}\");", htmlAttr["tabTitle"]);
                htmlAttr.Remove("tabTitle");
            }
            else
            {
                htmlAttr["onclick"] = ClickHandler;
            }
            return htmlHelper.ActionLink(linkText, actionName, controllerName, new RouteValueDictionary(values), htmlAttr);
        }

        /// <summary>
        /// 添加超链接，页面将在新标签页打开
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="controllerName"></param>
        /// <param name="values"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static string ActionLinkInNewTab(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary values, RouteValueDictionary htmlAttributes)
        {
            htmlAttributes["target"] = TargetName;
            if (htmlAttributes.ContainsKey("tabTitle"))
            {
                htmlAttributes["onclick"] = string.Format("return openLink(this, \"{0}\");", htmlAttributes["tabTitle"]);
                htmlAttributes.Remove("tabTitle");
            }
            else
            {
                htmlAttributes["onclick"] = ClickHandler;
            }
            return htmlHelper.ActionLink(linkText, actionName, controllerName, values, htmlAttributes);
        }

        /// <summary>
        /// 添加超链接，页面将在新标签页打开
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="controllerName"></param>
        /// <param name="protocol"></param>
        /// <param name="hostName"></param>
        /// <param name="fragment"></param>
        /// <param name="values"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static string ActionLinkInNewTab(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, object values, object htmlAttributes)
        {
            RouteValueDictionary htmlAttr = new RouteValueDictionary(htmlAttributes);
            htmlAttr["target"] = TargetName;
            if (htmlAttr.ContainsKey("tabTitle"))
            {
                htmlAttr["onclick"] = string.Format("return openLink(this, \"{0}\");", htmlAttr["tabTitle"]);
                htmlAttr.Remove("tabTitle");
            }
            else
            {
                htmlAttr["onclick"] = ClickHandler;
            }
            return htmlHelper.ActionLink(linkText, actionName, controllerName, protocol, hostName, fragment, new RouteValueDictionary(values), htmlAttr);
        }

        /// <summary>
        /// 添加超链接，页面将在新标签页打开
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="controllerName"></param>
        /// <param name="protocol"></param>
        /// <param name="hostName"></param>
        /// <param name="fragment"></param>
        /// <param name="values"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static string ActionLinkInNewTab(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary values, RouteValueDictionary htmlAttributes)
        {
            htmlAttributes["target"] = TargetName;
            if (htmlAttributes.ContainsKey("tabTitle"))
            {
                htmlAttributes["onclick"] = string.Format("return openLink(this, \"{0}\");", htmlAttributes["tabTitle"]);
                htmlAttributes.Remove("tabTitle");
            }
            else
            {
                htmlAttributes["onclick"] = ClickHandler;
            }
            return htmlHelper.ActionLink(linkText, actionName, controllerName, protocol, hostName, fragment, values, htmlAttributes);
        }
    }
}
